<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <title>在线辣鸡降重</title>
  </head>
  <body>
  <hr />
  <h1 align="center">在线辣鸡降重 -- 427宿舍专用</h1>
  <p align="right">Powered BY:  <a href="https://agou-ops.top">AGou-ops</a></p>
  <hr/>
    <div>
      <label for="appid">appid</label>
      <input type="text" class="appid" id="appid" value="20200507000442397" />

      <label for="key">秘钥</label>
      <input type="text" class="key" id="key" value="o3KZgYq7AlAgRtyD6IU0"/>

      <label for="level">去重模式</label>
      <select name="level" id="" class="level">
        <option value="simple">模式1</option>
        <option value="middle">模式2</option>
        <option value="high">模式3</option>
      </select>

    </div>
    <hr />
    <div>
      <textarea name="" class="fromText" cols="70" rows="20" style="font-size:16px;"></textarea>
      <button class="switchBtn">降重</button>
      <textarea name="" class="toText" cols="70" rows="20" style="font-size:16px;"></textarea>
    </div>
<p style="position:absolute; bottom:0; right:0;color:red;">岂能尽如人意，但求无愧我心。</p>


    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
	<script crossorigin="anonymous" integrity="sha512-TI/xDZMW8Z1RRljQdfZt1t6ojCe3TI8Kch85eOXll83KjYcc3JAQXakxpAvy1fU/c/UJtGMv+9kjndFP4InaAg==" src="https://lib.baomitu.com/layer/3.4.0/layer.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/blueimp-md5/2.18.0/js/md5.js"></script>
	
	<script>
;!function(){


//页面一打开就执行，放入ready是为了layer所需配件（css、扩展模块）加载完毕
layer.ready(function(){ 

layer.alert('百度API，反应迟钝，中级模式和高级模式需多等待。(5s后自动关闭)', {
  skin: 'layui-layer-molv' //样式类名
  ,closeBtn: 0,
  time: 5000
});

// layer.open({
  // type: 1,
  // shade: false,
  // title: false, //不显示标题
  // content: "  简单	中->英->德->中  \n 中级	中->英->德->中 高级	中->英->德->日->葡萄牙->意大利->波兰->保加利亚->爱沙尼亚->中",
  layer.tab({
  area: ['600px', '300px'],
  tab: [{
    title: '模式1', 
    content: '（仅供参考）中->英德->中'
  }, {
    title: '模式2', 
    content: '（仅供参考）中->韩德法->中'
  }, {
    title: '模式3', 
    content: '（仅供参考）中->日韩俄->中'
  }]
});
  
  
});


}();
</script>
	
	
	
	
    <script src="./script.js"></script>

    <script>
      var fromTextEle = $(".fromText"),
        toTextELe = $(".toText"),
        switchBtnEle = $(".switchBtn"),
        appid = "20210323000739360",
        key = "82GT5f3GpFEjNsNBPF1c";
      /*
简单	中->英->德->中
中级	中->英->德->日->葡萄牙->中
高级	中->英->德->日->葡萄牙->意大利->波兰->保加利亚->爱沙尼亚->中
*/
      var transList = {
        simple: ["zh en", "en de", "de zh"],
        middle: ["zh kor", "kor de", "de fra", "fra zh"],
        high: ["zh jp", "jp kor","kor ru", "ru zh"],
      };

      var mode = "simple";

      // 生成API参数
      function genParams(q, from, to) {
        var url = "http://api.fanyi.baidu.com/api/trans/vip/translate";
        var data = {
          q: q,
          from: from,
          to: to,
          appid: appid,
          salt: "",
          sign: "",
        };

        data.salt = Math.floor(Math.random() * 10000000000);
        data.sign = md5(appid + q + data.salt + key);

        return { url, data };
      }

      // 翻译
      function translate(q, from, to) {
        var params = genParams(q, from, to);

        return new Promise((resolve, reject) => {
          $.ajax({
            type: "POST",
            url: params.url,
            data: params.data,
            dataType: "jsonp",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            success: (res) => {
              log("from: " + from, "; ", "to:" + to);
              resolve(res);
            },
            error: () => {
              reject();
            },
          });
        });
      }

      // 翻译队列
      function queen(str) {
        var dst = str;
        (async () => {
          for (let i = 0; i < transList[mode].length; i++) {
            var lang = transList[mode][i].split(" ");
            res = await translate(dst, lang[0], lang[1]);
            dst = res.trans_result[0].dst;
            log("result", dst);
          }
          toTextELe.text(dst);
        })();
      }

      // 开始翻译
      function starSwitch() {
        appid = $(".appid").val();
        key = $(".key").val();
		// appid = 20200507000442397;
		// key = o3KZgYq7AlAgRtyD6IU0;
        if (fromTextEle.val() === "" || appid === "" || key === "") {
          return false;
        }

        mode = $(".level option:selected").val();

        queen(fromTextEle.val());
      }

      // 日志
      function log(...params) {
        var insertStr = params.join("");
        logEle = $(".log");
        logEle.text(logEle.text() + insertStr + "\n");
      }

      $(".switchBtn").click(starSwitch);
    </script>
  </body>
</html>
